package com.xianyun.book.command;

import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.book.convertor.BookConvertor;
import com.xianyun.book.domain.book.Book;
import com.xianyun.book.domain.gateway.BookGateway;
import com.xianyun.book.dto.clientobject.BookInfoCO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @author macos-zyj
 */
@Slf4j
@Component
public class BookCheckAddCmdExe {
    @Resource
    private BookGateway bookGateway;

    public SingleResponse<BookInfoCO> execute(String isbn){
        Book resultBook = bookGateway.saveOrQueryBookByIsbn(isbn);
        if ( null != resultBook){
            log.info("书籍查询 ISBN:{},查询结果: 书名:{}",isbn,resultBook.getBookName());
            return SingleResponse.of(BookConvertor.toDataCo(resultBook));
        } else {
            log.info("书籍查询 ISBN:{},查询结果: 书籍不存在",isbn);
            return SingleResponse.buildFailure("BOOK_NOT_FOUND","书籍不存在！");
        }
    }
}
